package com.jiashihui.schema_lgorithm.schema.singleton;

import java.util.concurrent.atomic.AtomicReference;

public class AtomicReferenceTest {
    public static void main(String[] args) {
        // 1. tomicReference atomicReference = new AtomicReference();
        // 2. String initialReference = "the initially referenced string";
        //    AtomicReference atomicReference = new AtomicReference(initialReference);
        // 3. AtomicReference<String> atomicStringReference = new AtomicReference<String>();

//        AtomicReference atomicReference = new AtomicReference("first value referenced");
//        String reference = (String) atomicReference.get();
//        System.out.println(reference);
//
//        AtomicReference atomicReference2 = new AtomicReference();
//        atomicReference2.set("New object referenced");
//        System.out.println(atomicReference2.get());

        String initialReference = "initial value referenced";
        AtomicReference<String> atomicStringReference = new AtomicReference<String>(initialReference);

        String newReference = "new value referenced";
        boolean exchanged = atomicStringReference.compareAndSet(initialReference, newReference);
        System.out.println("exchanged: " + exchanged);

        exchanged = atomicStringReference.compareAndSet(initialReference, newReference);
        System.out.println("exchanged: " + exchanged);

    }
}
